// pages/call/answer/answer.js
const httpUtils = require('../../../utils/httpUtil')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    callId: '',
    // 喊话类型和内容
    callType: 0,
    callContent: '请所有人员立即到会议室集合',
    callDeviceIds: [],
    // 应答列表
    responseList: [
      // { name: '张三短发大法师打发啊打', status: 'answer', time: '10:20' },
      // { name: '李四', status: 'no-resp', time: '00:00' },
      // { name: '王五', status: 'answer', time: '10:22' },
      // { name: '赵六', status: 'no-resp', time: '00:00' }
    ],
    isCalling: true,
    isLoading: true,
    updateTime: '00:00:00',
    timer: null,
    history: false // 历史应答记录
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      callId: options.callId,
      callType: options.callType,
      callContent: options.callContent,
      callDeviceIds: [options.callDeviceIds],
      history: options.history ?? false
    })
    this.getAnswerList(1, 10, this.data.callId);

    if (!this.data.history) {
      this.setData({
        timer: setInterval(() => {
          this.getAnswerList(1, 10, this.data.callId);
        }, 10000)
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (!this.data.history) {
      clearInterval(this.data.timer);
      if (this.data.isCalling) {
        this.hangup();
      }
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  getAnswerList(page, size, callId) {
    this.setData({
      isLoading: true
    });
    httpUtils.request({
      'method': 'GET',
      'url': '/prod-api/shout/shout/replylist?pageNum='+page+'&pageSize='+size+'&callId='+callId,
    }).then(res=>{
      if (res.data.code == 200) {
        this.setData({
          isLoading: false,
          updateTime: this.getCurrentTime(),
          responseList: res.data.rows.map(replay=>{
            return {
              name: replay.number,
              status: replay.replyStatus == 1 ? 'answer' : 'no-resp',
              time: replay.replyTime != null ? this.data.history ? replay.replyTime : replay.replyTime.slice(-8) : '00:00:00'
            }
          })
        });
      } else {
        this.setData({
          isLoading: false,
        })
      }
    })
  },

  // 停止喊话
  endCall() {
    var that = this;
    wx.showModal({
      content: '确定要停止当前喊话吗？',
      success(res) {
        if (res.confirm) {
          that.hangup();
        }
      }
    })
  },

  hangup() {
    if (this.data.callType == 0) {
      httpUtils.request({
        'method': 'POST',
        'url': '/prod-api/interface/sendSms',
        'showLoading': true,
        'messages': '停止喊话',
        'data': {
          'numbers': this.data.callDeviceIds,
          'type': 2,
        }
      }).then(res=>{
        if (res.data.code == 200) {
          this.setData({
            isCalling: false
          });
          wx.navigateBack();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'error'
          });
        }
      }).catch(e => {
        wx.showToast({
          title: '挂断出错',
          icon: 'error'
        })
      });
    } else {
      httpUtils.request({
        'method': 'POST',
        'url': '/prod-api/interface/hangUpText',
        'showLoading': true,
        'message': '挂断中',
        'data': {
          'callIds': this.data.callDeviceIds
        }
      }).then(res => {
        if (res.data.code == 200) {
          this.setData({
            isCalling: false
          });
          wx.navigateBack();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'error'
          });
        }
      }).catch(e => {
        wx.showToast({
          title: '挂断出错',
          icon: 'error'
        })
      });
    }
  },

  getCurrentTime() {
    const now = new Date();
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
    return `${hours}:${minutes}:${seconds}`;
  },
})